package com.rightsidetech.commom.utils.gps;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rightsidetech.commom.config.GaoDeConfig;
import com.rightsidetech.commom.utils.HttpClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.awt.*;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述
 *
 * @description 地图上点在范围内计算
 */
@Slf4j
public class MapUtils {



    public static boolean check(double ALon , double ALat , Point[] ps){

        int iSum, iCount, iIndex;
        double dLon1 = 0, dLon2 = 0, dLat1 = 0, dLat2 = 0, dLon;
        if (ps.length < 3) {
            return false;
        }
        iSum = 0;
        iCount = ps.length;
        for (iIndex = 0; iIndex < iCount; iIndex++) {
            if (iIndex == iCount - 1) {
                dLon1 = ps[iIndex].getX();
                dLat1 = ps[iIndex].getY();
                dLon2 = ps[0].getX();
                dLat2 = ps[0].getY();
            } else {
                dLon1 = ps[iIndex].getX();
                dLat1 = ps[iIndex].getY();
                dLon2 = ps[iIndex + 1].getX();
                dLat2 = ps[iIndex + 1].getY();
            }
            // 以下语句判断A点是否在边的两端点的水平平行线之间，在则可能有交点，开始判断交点是否在左射线上
            if (((ALat >= dLat1) && (ALat < dLat2)) || ((ALat >= dLat2) && (ALat < dLat1))) {
                if (Math.abs(dLat1 - dLat2) > 0) {
                    //得到 A点向左射线与边的交点的x坐标：
                    dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - ALat)) / (dLat1 - dLat2);
                    // 如果交点在A点左侧（说明是做射线与 边的交点），则射线与边的全部交点数加一：
                    if (dLon < ALon) {
                        iSum++;
                    }
                }
            }
        }
        if ((iSum % 2) != 0) {
            return true;
        }
        return false;
    }

    /**
     * 高德api获取真实距离(电动车)
     * */
    public static Map<String, String> getAMapApi(GaoDeConfig gaoDeConfig, BigDecimal longitude, BigDecimal latitude, BigDecimal targetLongitude, BigDecimal targetLatitude) {
        String origin = longitude.setScale(6,BigDecimal.ROUND_DOWN)+","+latitude.setScale(6,BigDecimal.ROUND_DOWN);
        String destination = targetLongitude.setScale(6,BigDecimal.ROUND_DOWN)+","+targetLatitude.setScale(6,BigDecimal.ROUND_DOWN);

        Map<String, String> map = new HashMap<>();
        Map<String, String> resultMap = new HashMap<>();
        map.put("key", gaoDeConfig.getAmapKey());
        map.put("origin",origin);
        map.put("destination",destination);
        map.put("show_fields","cost");
        try {
            Double distance;
//            Double duration;
            JSONObject jsonObject = JSON.parseObject(HttpClient.sendGet(gaoDeConfig.getAmapUrl(),map));
            if (!ObjectUtils.isEmpty(jsonObject) && "1".equals(jsonObject.get("status").toString())){
                JSONObject route = jsonObject.getJSONObject("route");
                JSONObject paths = (JSONObject) route.getJSONArray("paths").get(0);
//                String jsonObjString = paths.getString("steps");
                distance = Double.valueOf(paths.get("distance").toString());
                log.info("高德API距离计算,distance ={}",distance);
            }else {
                log.info("高德API未成功获取真实距离,使用直线距离计算");
                distance = GPSUtil.getDistance(latitude.doubleValue(), longitude.doubleValue(), targetLatitude.doubleValue(), targetLongitude.doubleValue());
            }
            resultMap.put("distance", String.valueOf((int)Math.ceil(distance)));
        } catch (Exception e) {
            log.error("获取距离时间错误",e.getMessage());
        }
        return resultMap;
    }
}
